;;; primitives.scm -- primitive operators for Joy.
;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
;;;
;;; Joy is free software; you can redistribute it and/or modify it under
;;; the terms of the GNU General Public License as published by the Free
;;; Software Foundation; either version 3 of the License, or (at your
;;; option) any later version.
;;;
;;; Joy is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
;;; License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Joy.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;;
;;; All Joy procedures take a stack argument and return a stack
;;; argument.  It's convenient for primitives to use Scheme "rest"
;;; arguments to deconstruct the expected number of arguments.

(define-module (language joy primitives)
  #:use-module ((system base compile)
		#:select (compiled-file-name compile-file))
  #:use-module ((guile)
		#:select (load-compiled string-append and=>))
  #:use-module ((ice-9 safe-r5rs)
		#:select (+ - < > eqv? display list->string)
		#:renamer (symbol-prefix-proc '%))
  #:use-module ((srfi srfi-1)
		#:select (cons fold every)
		#:renamer (symbol-prefix-proc '%))
  #:use-module (srfi srfi-2)
  #:use-module (language joy write)
  #:replace (cons + - < > =)
  #:export (uncons
	    swap
	    dup
	    pop
	    choice
	    infra
	    stack
	    unstack

	    ;; IO
	    putch
	    putchars
	    write
	    #{.}#
	    include

	    ;; datatype inquiry
	    logical
	    char
	    integer
	    string
	    list

	    ;; variable definition
	    def

            exit))

;;; Code:

(define (->truth b)
  (if b 'true 'false))

;;; TODO: This could be written in base in terms of '='.
(define (logical x . S)
  (%cons (->truth (if (eq? x 'true)
                      #t
                      (eq? x 'false))) S))

(define (char x . S)
  (%cons (->truth (char? x)) S))

(define (integer x . S)
  (%cons (->truth (integer? x)) S))

(define (string x . S)
  (%cons (->truth (and (list? x) (%every char? x)))
	 S))

(define (list x . S)
  (%cons (->truth (list? x)) S))

(define (cons lst x . S)
  (%cons (%cons x lst) S))

(define (uncons lst . S)
  (%cons (cdr lst) (%cons (car lst) S)))

(define (swap x y . S)
  (%cons y (%cons x S)))

(define (dup x . S)
  (%cons x (%cons x S)))

(define (pop _ . S)
  S)

(define (+ x y . S)
  (%cons (%+ y x) S))

(define (- x y . S)
  (%cons (%- y x) S))

(define (< x y . S)
  (%cons (->truth (%< y x)) S))

(define (> x y . S)
  (%cons (->truth (%> y x)) S))

(define (= x y . S)
  (%cons (->truth (%eqv? x y)) S))

(define (choice y x b . S)
  (%cons (if (eq? b 'true) x y) S))

(define (infra q lst . S)
  (%cons (%fold (@ (language joy eval) eval) lst q)
	 S))

(define (stack . S)
  (%cons S S))

(define (unstack S . _)
  S)

(define (putch c . S)
  (write-char c)
  S)

;;; TODO: This could be written in base in terms of putch.
(define (putchars x . S)
  (%display (list->string x))
  S)

(define (write x . S)
  (write-joy x)
  S)
(define #{.}# write)

(define (include str . S)
  (and-let* ((s (list->string str))
	     (f (search-path %load-path s '("" ".joy")))
	     (go (compiled-file-name f)))
    (if go
	(begin
	  (compile-file f #:output-file go #:from 'joy)
	  (apply (load-compiled go) S))
	(error "could not find file to include:" s))))

(define (def body sym . S)
  (module-define! (resolve-module '(joy))
		  (car sym)		;item is list-quoted
		  body)
  S)

(define (exit status . _)
  "Immediately exit the program with STATUS."
  (primitive-exit status))

;;; For efficiency, having low-level implementations of the following
;;; might be beneficial (though I have yet to prove this in practice):
;;;
;;; i dip dipd popd dupd swapd times divmod / * % <= >= max min true false and
;;; or not null branch ifte
;;;
;;; In particular, it seems the dip and i combinators would be
;;; especially beneficial.  Or just dip if we use 'i == dup dip pop'.
